package com.manage;

import java.util.ArrayList;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.manage.adapter.OrderAdapter;
import com.manage.base.BaseActivity;
import com.manage.model.Order;
import com.manage.model.OrderItem;

/**
 * 历史订单详情 2012-6-19
 * 参考OrderDetailActivity
 * @author liyoro@163.com
 * 
 */
public class HistoryOrderDetailActivity extends BaseActivity {

	private static String TAG = "HistoryOrderDetailActivity";
	
	private Order order = null;

	private ArrayList<OrderItem> orderList = new ArrayList();
	private OrderAdapter orderAdapter = null;

	// top
	private ListView caipinList = null;

	// center
	private TextView tvPrice = null;
	private TextView tvOrderAdditional = null;

	// bottom
	private TextView tvClientName = null;
	private TextView tvPhoneNumber = null;
	private TextView tvClientcBuinding = null;
	private TextView tvClientAddress = null;
	private TextView tvClientOrderTime = null;
	
	//
	private LinearLayout layout_remard = null;
	private LinearLayout layout_evaluation = null;
	private LinearLayout layout_modify_record = null;
	
	private LinearLayout delContainer = null;
	private LinearLayout addContainer = null;
	private ArrayList<OrderItem> delorderList = new ArrayList();
	private ArrayList<OrderItem> addorderList = new ArrayList();
	

	private TextView tvTaste = null;
	private TextView tvSpeed = null;
	private TextView tvService = null;
	private TextView tvRemark = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_history_order_details);
		initDatas();
	}
	
	@Override
	public String getLeftBtnName() {
		return "返回";
	}

	@Override
	public String getTitleName() {
		return "订单详情";
	}

	@Override
	public boolean showButton() {
		return true;
	}

	@Override
	public void initView() {
		caipinList = (ListView) findViewById(R.id.listview1);

		orderAdapter = new OrderAdapter(HistoryOrderDetailActivity.this);
		caipinList.setAdapter(orderAdapter);
		
		tvPrice = (TextView) findViewById(R.id.total_price);
		tvOrderAdditional = (TextView) findViewById(R.id.order_additional);

		tvClientName = (TextView) findViewById(R.id.client_name);
		tvPhoneNumber = (TextView) findViewById(R.id.phone_number);
		tvClientcBuinding = (TextView) findViewById(R.id.client_buinding);
		tvClientAddress = (TextView) findViewById(R.id.client_address);
		tvClientOrderTime = (TextView) findViewById(R.id.order_time);
		
		layout_remard  = (LinearLayout)findViewById(R.id.layout_remard);
		layout_evaluation  = (LinearLayout)findViewById(R.id.layout_evaluation);
		layout_modify_record  = (LinearLayout)findViewById(R.id.layout_modify_record);
		
		delContainer = (LinearLayout)findViewById(R.id.layout_del_record);
		addContainer = (LinearLayout)findViewById(R.id.layout_add_record);

		tvTaste = (TextView) findViewById(R.id.tv_taste);
		tvSpeed = (TextView) findViewById(R.id.tv_speed);
		tvService = (TextView) findViewById(R.id.tv_service);
		tvRemark = (TextView) findViewById(R.id.tv_remark);
	}

	@Override
	public void initDatas() {
		order =  (Order)getIntent().getSerializableExtra(ManageAcceptedActivity.SER_KEY);
		
		if(order != null) {
			tvOrderAdditional.setText(order.getClientAddition());
			tvClientName.setText(order.getClientName());
			tvPhoneNumber.setText(order.getPhoneNumber());
			tvClientcBuinding.setText(order.getClientBuilding());
			tvClientAddress.setText(order.getClientAddress());
			tvClientOrderTime.setText(order.getOrderTime());
			
			orderList = order.getDishes();
			
			orderAdapter.setValue(orderList);
			
			delorderList = order.getDelDishes();
			
			if(delorderList != null) {
				if(delorderList.size() > 0) {
					layout_modify_record.setVisibility(View.VISIBLE);
					for(int i=0; i<delorderList.size(); i++) {
						OrderItem delItem = delorderList.get(i);
						String strdel = delItem.getCaipin_count() + "份" + delItem.getCaipin_name() + "；";
						
						TextView tvDel = new TextView(this);
						tvDel.setText(strdel);
						RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,     
				                   LinearLayout.LayoutParams.WRAP_CONTENT);
						param.setMargins(60, 0, 0, 0);
						tvDel.setLayoutParams(param);
						
						delContainer.addView(tvDel);
					}
				} else {
					layout_modify_record.setVisibility(View.GONE);
				}
			}
			
			addorderList = order.getAddDishes();
			
			if(addorderList != null) {
				if(addorderList.size() > 0) {
					layout_modify_record.setVisibility(View.VISIBLE);
					for(int i=0; i<addorderList.size(); i++) {
						OrderItem delItem = addorderList.get(i);
						String strdel = delItem.getCaipin_count() + "份" + delItem.getCaipin_name() + "；";
						
						TextView tvDel = new TextView(this);
						tvDel.setText(strdel);
						RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,     
				                   LinearLayout.LayoutParams.WRAP_CONTENT);
						param.setMargins(60, 0, 0, 0);
						tvDel.setLayoutParams(param);
						
						addContainer.addView(tvDel);
					}
				} else {
					layout_modify_record.setVisibility(View.GONE);
				}
			}
			
			Log.i(TAG, "order.getTaste():"+order.getTaste());
			if(order.getTaste().length() > 0 && order.getSpeed().length() > 0 && order.getService().length() > 0) {
				layout_evaluation.setVisibility(View.VISIBLE);
				tvTaste.setText(order.getTaste());
				tvSpeed.setText(order.getSpeed());
				tvService.setText(order.getService());
			} else {
				layout_evaluation.setVisibility(View.GONE);
			}
			
			if(order.getRemark() != "") {
				layout_remard.setVisibility(View.VISIBLE);
				tvRemark.setText(order.getRemark());
			} else {
				layout_remard.setVisibility(View.GONE);
			}
		}
		tvPrice.setText(orderAdapter.getTotalPrice());
		
	}
}
